package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.AdminLogConfig;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.AdminLogConfigMapper;
import com.zrwl.poscloud.xo.service.AdminLogConfigService;
import com.zrwl.poscloud.xo.vo.AdminLogConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 操作员日志配置 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-11-23
 */
@Service
public class AdminLogConfigServiceImpl extends SuperServiceImpl<AdminLogConfigMapper,AdminLogConfig> implements AdminLogConfigService {

    @Autowired
    private AdminLogConfigService adminLogConfigService;
    @Override
    public IPage<AdminLogConfig> getPageList(AdminLogConfigVO adminLogConfigVO){
       QueryWrapper<AdminLogConfig> queryWrapper = new QueryWrapper<>();
       if (StringUtils.isNotEmpty(adminLogConfigVO.getKeyword()) && !StringUtils.isEmpty(adminLogConfigVO.getKeyword().trim())) {
            queryWrapper.like("name", adminLogConfigVO.getKeyword().trim());
       }
       if(adminLogConfigVO.getLogLevel() != 0){
           queryWrapper.eq("log_level", adminLogConfigVO.getLogLevel());
       }
        if(adminLogConfigVO.getBusinessType() != 0){
            queryWrapper.eq("business_type", adminLogConfigVO.getBusinessType());
        }
        if(adminLogConfigVO.getOperationType() != 0){
            queryWrapper.eq("operation_type", adminLogConfigVO.getOperationType());
        }
       Page<AdminLogConfig> page = new Page<>();
       page.setCurrent(adminLogConfigVO.getCurrentPage());
       page.setSize(adminLogConfigVO.getPageSize());
       queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
       queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
       IPage<AdminLogConfig> pageList = adminLogConfigService.page(page, queryWrapper);
       List<AdminLogConfig> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<AdminLogConfig> getAllList(AdminLogConfigVO adminLogConfigVO){
        QueryWrapper<AdminLogConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<AdminLogConfig> list = adminLogConfigService.list(queryWrapper);
        return list;
}
    @Override
    public String add(AdminLogConfigVO adminLogConfigVO){
        adminLogConfigVO.setCurrentPage(1l);
        adminLogConfigVO.setPageSize(100l);
        List<AdminLogConfig> records = adminLogConfigService.getPageList(adminLogConfigVO).getRecords();
        if(!records.isEmpty()){
            return ResultUtil.errorWithData("重复数据");
        }
        AdminLogConfig entity = new AdminLogConfig();
        BeanUtils.copyProperties(adminLogConfigVO, entity, SysConf.STATUS);
        entity.insert();
        adminLogConfigVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(AdminLogConfigVO adminLogConfigVO){
        AdminLogConfig entity = adminLogConfigService.getById(adminLogConfigVO.getUid());
        BeanUtils.copyProperties(adminLogConfigVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(AdminLogConfigVO adminLogConfigVO){
        AdminLogConfig entity = adminLogConfigService.getById(adminLogConfigVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
}
